/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package auth;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

/**
 *
 * @author root
 */
@WebService()
public class Authentification {

    /**
     * Web service operation
     */
    String bon_login = "admin";
    String bon_password = "adminadmin";
    String loginFichier;
    String passwordFichier;
    Boolean result = false;
    private BufferedWriter out;
    private BufferedReader in;
    boolean trouve = false;
    Date maDate = new Date();

    @WebMethod(operationName = "auth_operation")
    public Boolean auth_operation(@WebParam(name = "login")
    String login, @WebParam(name = "password")
    String password) {

        //Verification basique
        if (login.equals(bon_login) && password.equals(bon_password)){
            result = true;
        }
        else {
            result = false;
        }

        //Le code ci-dessous permet l'authentification en vérifiant que le nom d'utilisateur et le password
        //existent dans un fichier (=bdd)

        /*ArrayList<String> list = new ArrayList<String>();
        try {
            in = new BufferedReader(new FileReader(new File("/home/conf/usersDB")));

            while (in.ready() && !trouve) {
                String ligne = in.readLine();
                Scanner s = new Scanner(ligne).useDelimiter("\\s* \\s*");

                loginFichier = s.next();
                passwordFichier = s.next();

                if (loginFichier.equals(login) && passwordFichier.equals(password)){
                    trouve = true;
                    result = true;
                }

            }
        } catch (IOException ex) {
            Logger.getLogger(Authentification.class.getName()).log(Level.SEVERE, null, ex);
        }

        try{
            out = new BufferedWriter(new FileWriter(new File("/home/logs/authentification.log"), true));
            out.write(maDate.toString()+" | login : "+login+" | password : "+password+" | result : "+result.toString());
            out.newLine();
            out.flush();
        } catch (IOException ex) {
            Logger.getLogger(Authentification.class.getName()).log(Level.SEVERE, null, ex);
        }*/

        return result;
    }

}
